
import React from 'react';
import * as THREE from 'three';
import Controller from '../../Controller';

export default function useRender(controller: Controller) {

    /** 渲染器 */
    const renderer = controller.useMemo(s => s.renderer, []);

    /** 场景 */
    const scene = controller.useMemo(s => s.scene, []);

    /** 摄像机 */
    const camera = controller.useMemo(s => s.camera, []);

    // 渲染图像
    React.useEffect(() => {
        function animate() {
            requestAnimationFrame(animate);
            if (!renderer || !scene || !camera) return;
            renderer.render(scene, camera);
            scene.children.forEach(e => {
                if (e instanceof THREE.SpotLightHelper) {
                    e.update()
                }
            })
        }
        animate();
    }, [camera, renderer, scene])
}